// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// N [author] rhyme_lph
// E [email]  rhymelph@gmail.com
// B [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/page/setting/subscription/sub_billing_view.dart';
import 'package:finance_app/src/page/setting/subscription/sub_plans_view.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/common_tab_bar.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/setting/subscription_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter_svg/flutter_svg.dart';

@RouterMeta(paramName: "subscriptionPage")
class SubscriptionPage extends StatefulWidget {
  final int tabIndex;

  const SubscriptionPage({Key key, this.tabIndex}) : super(key: key);
  @override
  _SubscriptionPageState createState() => _SubscriptionPageState();
}

class _SubscriptionPageState extends State<SubscriptionPage>
    with
        PagePresenter<SubscriptionPage, SubscriptionProvider>,
        SingleTickerProviderStateMixin {
  @override
  void initState() {
    provider.controller = TabController(
        initialIndex: widget.tabIndex ?? 0, length: 2, vsync: this);
    super.initState();
  }

  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.subscription, actions: [
        listBuilder<List<String>>(
          selector: (_, __) => List.from(__.menuList),
          builder: (BuildContext context, List<String> value, Widget child) =>
              value.length == 0
                  ? Container(
                      width: getDp(6),
                    )
                  : NormalMorePopupButton(
                      menuList: value,
                      onSelect: provider.onSelectMenu,
                    ),
        )
      ]);

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: DefaultTabController(
          length: 2,
          child: Column(
            children: [
              if (Config.currentSubscription.messages.length > 0)
                Container(
                  decoration: BoxDecoration(
                    color: aceLightBlue,
                  ),
                  child: Column(
                      children: Config.currentSubscription.messages
                          .map(
                            (e) => Padding(
                              padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                              child: Row(
                                children: [
                                  SvgPicture.asset(A.assetsImagesSubInfo),
                                  AppSpaces.w15,
                                  Expanded(
                                    child: Text(
                                      e.info,
                                      style: TextStyle(color: aceBlueColor),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          )
                          .toList()),
                ),
              listBuilder<bool>(
                  selector: (_, __) => __.isShowBill,
                  builder: (BuildContext context, bool value, Widget child) =>
                      Visibility(
                        child: child,
                        visible: value,
                      ),
                  child: CommonTabBar(
                    controller: provider.controller,
                    tabs: provider.tabs
                        .map((e) => MyTab(
                              text: e,
                            ))
                        .toList(),
                  )),
              Expanded(
                  child: listBuilder<bool>(
                selector: (_, __) => __.isShowBill,
                builder: (BuildContext context, bool value, Widget child) =>
                    value
                        ? TabBarView(
                            controller: provider.controller,
                            physics: value == true
                                ? null
                                : NeverScrollableScrollPhysics(),
                            children: [
                                SubBillingView(),
                                SubPlansView(),
                              ])
                        : SubPlansView(),
              )),
            ],
          ),
        ),
      );
}
